package com.dz.risk.common.utils;

import com.dz.risk.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author baohan
 * @className DateUtils
 * @description TODO
 * @date 2021/7/2 13:39
 */
@Slf4j
public class DateUtils {

    /**
     * yyyy-MM-dd to  date
     *
     * @param time
     * @return
     */
    public static Date stringToDate(String time) {
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("时间转换异常" + e.getMessage());
        }
        return parse;
    }

    /**
     * yyyy-MM to  date
     *
     * @param time
     * @return
     */
    public static Date stringToMonth(String time) {
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("时间转换异常" + e.getMessage());
        }
        return parse;
    }

    /**
     * yyyy-MM-dd HH:mm:ss to  date
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static Date stringToDate2(String time) {
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("时间转换异常" + e.getMessage());
            throw new RRException("时间转换异常" + e.getMessage());
        }
        return parse;
    }

    /**
     * 获取指定某一天的开始 00：00
     *
     * @return String date类型
     */
    public static String getDailyStartTimeToDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(getCalendarStart().getTime());
    }

    /**
     * 获取指定某一天的结束 23：59
     *
     * @return String date类型
     */
    public static String getDailyEndTimeToDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(getCalendarEnd().getTime());
    }

    /**
     * 获取当前时间(返回格式：yyyy-MM-dd HH:mm:ss)
     *
     * @return
     * @throws Exception
     */
    public static String getTime1() {
        Date date = new Date();
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // new Date()为获取当前系统时间
        String time = df.format(date);
        return time;
    }


    /**
     * 获取当前时间(返回格式：yyyy-MM-dd)
     *
     * @return
     * @throws Exception
     */
    public static String getTime2() {
        Date date = new Date();
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // new Date()为获取当前系统时间
        String time = df.format(date);
        return time;
    }

    /**
     * yyyyMMdd 转 yyyy-MM-dd
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String stringToDateString(String str) {
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
        String sfstr = null;
        try {
            sfstr = sf2.format(sf1.parse(str));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RRException("时间转换异常" + e.getMessage());

        }
        return sfstr;
    }

    /**
     * date转字符串(yyyy-MM-dd HH:mm:ss)
     *
     * @param date 日期
     * @return
     */
    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * date转数字类型(yyyyMM)
     *
     * @param date 时间
     * @return 年月
     */
    public static Long dateToLongYears(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return Long.parseLong(sdf.format(date));
    }

    /**
     * date转数字类型(yyyy-MM-dd)
     *
     * @param date 时间
     * @return 年月
     */
    public static String dateToStringYears(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * date转数字类型(yyyy-MM)
     *
     * @param date 时间
     * @return 年月
     */
    public static String dateToStringMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(date);
    }

    /**
     * date转数字类型(yyyyMMdd)
     *
     * @param date 时间
     * @return 数字年月日
     */
    public static Long dateToLongDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Long.parseLong(sdf.format(date));
    }

    public static Calendar getCalendarStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    public static Calendar getCalendarEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar;
    }
}
